package com.ceo.common.export;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author uto_dev_008
 * @desc
 * @since 2022/1/11 14:11
 * Copyright (c)  All rights reserved
 */
public class TableOption {
    public static enum ACTION {SELECTED,CURRENTPAGE,ALL}
    private String sheetName="sheet1";
    private TableMapper tableMapper;
    private List<?> data;
    private Object countData;
    private List<String> bottomInfo;
    private List<Map> headers;
    private String keyField="dataIndex";
    private String keyName="title";
    public TableOption(List<Map> headerConfig){
        this.headers=headerConfig;
        transformTableMapper();
    }

    /**
     * 转换表格导出信息为TableMapper
     * @return
     */
    private void transformTableMapper()
    {
        TableMapper tm=new TableMapper();
        for(Map m:headers){
            tm.addField(parseAntDTableHeander(m));
        }
        this.tableMapper=tm;
    }
    private TableHeader parseAntDTableHeander(Map headerMap){
        String title=(String)headerMap.get(keyName);
        String field=(String)headerMap.get(keyField);
        String dateFormat=(String)headerMap.get("dateFormat");
        List<Map> _enum=(List<Map>)headerMap.get("_enum");
        TableHeader tf=new TableHeader(field,title);
        if(StrUtil.isNotBlank(dateFormat)){
            tf.setFormater(new DateFormater(dateFormat));
        }else if(_enum!=null){
            Map<String,String> _enum_m=new HashMap<>();
            for(Map m:_enum)
                _enum_m.put(MapUtil.getStr(m,"id"),MapUtil.getStr(m,"text"));
            tf.setFormater(new EnumFormater(_enum_m));
        }
        List<Map> childrens=(List<Map>)headerMap.get("children");
        if(childrens!=null){
            for (Map m:childrens)
                tf.addChildField(parseAntDTableHeander(m));
        }
        return tf;
    }
    public List<List<String>> getEasyHeader(){
        return tableMapper.convertHeader();
    }
    public List<List<Object>> getEasyData(){
        return tableMapper.convertData(data);
    }
    public String getSheetName() {
        return sheetName;
    }
    public void setSheetName(String title) {
        this.sheetName = sheetName;
    }
    public TableMapper getTableMapper() {
        return tableMapper;
    }
    public void setTm(TableMapper tableMapper) {
        this.tableMapper = tableMapper;
    }
    public List<?> getData() {
        return data;
    }
    public void setData(List<?> data) {
        this.data = data;
    }
    public Object getCountData() {
        return countData;
    }
    public void setCountData(Object countData) {
        this.countData = countData;
    }
    public List<String> getBottomInfo() {
        return bottomInfo;
    }
    public void setBottomInfo(List<String> bottomInfo) {
        this.bottomInfo = bottomInfo;
    }
}
